//
//  RegisterCommonCell.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/11/28.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "RegisterCommonCell.h"
#import "RegisterController.h"
#import "ResetPasswordVC.h"
#import "ModifyPasswordVC.h"
#import <LEEAlert/LEEAlert.h>
@interface RegisterCommonCell ()
@property (weak, nonatomic) IBOutlet UITextField *rightTextField;
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;
@property (weak, nonatomic) IBOutlet UILabel *topLabel;
@property (weak, nonatomic) IBOutlet UIView *lineView;
/** <#note#> */
@property (nonatomic, weak) UIImageView *securityCodeView;

@property (nonatomic, weak) NSTimer *timer;
@end

@implementation RegisterCommonCell

static NSInteger times = 60;

- (void)awakeFromNib {
    
    [super awakeFromNib];
    
    self.topLabel.font = MYFONT(15);
    self.lineView.hidden = YES;
    self.rightBtn.titleLabel.font = MYFONT(15);
    [self.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
    [self.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    self.rightBtn.layer.masksToBounds = YES;
    self.rightBtn.layer.cornerRadius = 5;
    self.rightBtn.backgroundColor = themeColor;
    self.lineView.backgroundColor = themeColor;
    self.rightTextField.font = MYFONT(15);
   [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.rightTextField];
    
}

- (void)setModel:(RegisterInfoModel *)model{
    _model = model;
    
    if (_model.userCaptcha.length > 0) {
        
        self.rightTextField.text = _model.userCaptcha;
    }
    
    
}


- (void)textChange{
    
    self.model.userCaptcha = self.rightTextField.text;
    
    if (self.rightTextField.text.length > 0) {
        
        [self.timer invalidate];
        
        self.rightBtn.enabled = YES;
        
        self.rightBtn.backgroundColor = themeColor;
        
        self.lineView.backgroundColor = themeColor;
        
        [self.rightBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        
        
    }
    
}



- (IBAction)rightBtnDidTap:(UIButton *)sender {
    
    if (self.model.email == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请填写手机号"];
        
        return;
    }
    
    BOOL isEmail = [[NetworkTools shareNetworkTools] isEmailAdress:self.model.email];
    
    if (!isEmail) {
        
        [SVProgressHUD showInfoWithStatus:@"手机号格式不正确!"];
        
        return;
        
    }
    
    [self cacheSecurityCode];
    
    [self alert];
    
}

- (void)cacheSecurityCode{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"mobile"] = self.model.email;
    MJWeakSelf
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"Member/createPicCaptcha" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
//            NSLog(@"%@",responseObject);
            
            weakSelf.model.picCaptchaUrl = responseObject[@"data"][@"picCaptchaUrl"];
            [[SDImageCache sharedImageCache]removeImageForKey:weakSelf.model.picCaptchaUrl];
            [weakSelf.securityCodeView sd_setImageWithURL:[NSURL URLWithString:weakSelf.model.picCaptchaUrl] placeholderImage:nil];
            
        }
    }];
    
}




- (void)alert{
    
    __block UITextField *myTextField = nil;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 62)];
    
    view.backgroundColor = [UIColor redColor];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:view.bounds];
    
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    imageView.backgroundColor = viewBgGrey;
    
    self.securityCodeView = imageView;
    
    [view addSubview:imageView];
    
    [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapAction:)]];
    
    
    MJWeakSelf;
    [LEEAlert alert].config
    .LeeTitle(@"验证码")
    .LeeAddCustomView(^(LEECustomView *custom) {
        
        custom.view = view;
        
        custom.positionType = LEECustomViewPositionTypeCenter;
        
    })
    .LeeItemInsets(UIEdgeInsetsMake(6, 0, 0, 0)) // 想为哪一项设置间距范围 直接在其后面设置即可 ()
    .LeeContent(nil)
    .LeeItemInsets(UIEdgeInsetsMake(8, 0, 0, 0)) // 这个间距范围就是对content设置的
    .LeeAddTextField(^(UITextField *textField) {
        
        myTextField = textField;
        textField.font = MYFONT(14);
        textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        textField.placeholder = @"输入验证码";
        
    })
    .LeeAction(@"取消", nil)
    .LeeCancelAction(@"确认", ^{
        
        
        NSLog(@"%@",myTextField.text);
        if (myTextField.text.length > 0) {
            
            [weakSelf sureBtnDidTapWith:myTextField.text];
            
        }
        
        // 确认点击事件Block
    })
    .LeeShow();
    
    
}





- (void)sureBtnDidTapWith:(NSString *)code{
    
    // 1.拼接参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"mobile"] = self.model.email;
    parameters[@"code"] = code;
    
    if (self.model.type != nil){
        
        parameters[@"type"] = @"1";
        
    }

    MJWeakSelf;
    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"Member/sendSms" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        MYJSONSTR

        if (!error) {
            
            
            //weakSelf.model.houTaiCaptcha = responseObject[@"data"];
            [weakSelf lastSure];
            
        }else{
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
            self.rightBtn.enabled = YES;
            
            self.rightBtn.backgroundColor = themeColor;
            
            self.lineView.backgroundColor = themeColor;
            
            [self.rightBtn setTitle:@"重新获取" forState:UIControlStateNormal];
            
        }
        
    }];
  
}







- (void)lastSure{
   
    self.rightBtn.backgroundColor = colorWithA(75, 75, 75, 0.2);
    
    self.lineView.backgroundColor = colorWithA(75, 75, 75, 0.2);
    
    self.rightBtn.enabled = NO;
    
    [self.rightBtn setTitle:[NSString stringWithFormat:@"已发送(%ds)",60] forState:UIControlStateNormal];
    
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(Countdown) userInfo:nil repeats:YES];
    
    
    if (self.model.type != nil) {
        
        if ([self.model.type isEqualToString:@"motify"]) {
           ModifyPasswordVC  *temp = (ModifyPasswordVC *)self.viewController;
            
            temp.timer = self.timer;
            
        } else {
            ResetPasswordVC *temp = (ResetPasswordVC *)self.viewController;
            
            temp.timer = self.timer;
            
        }
        

    } else {
        
        RegisterController *temp = (RegisterController *)self.viewController;
        
        temp.timer = self.timer;
    }
    
    
    
//
//    // 1.拼接参数
//    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
//    parameters[@"email"] = self.model.email;
//    MJWeakSelf;
//    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"Member/sendEmail" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
//
//
//
//        if (!error) {
//
//            MYJSONSTR
//
//            weakSelf.model.houTaiCaptcha = responseObject[@"data"];
//
//        }else{
//
//            [SVProgressHUD showInfoWithStatus:error.domain];
//
//            self.rightBtn.enabled = YES;
//
//            self.rightBtn.backgroundColor = themeColor;
//
//            self.lineView.backgroundColor = themeColor;
//
//            [self.rightBtn setTitle:@"重新获取" forState:UIControlStateNormal];
//            //            NSLog(@"%@",responseObject);
//        }
//
//    }];
//
//
//
//
//
    
}



- (void)viewTapAction:(UITapGestureRecognizer *)tap{

//    NSLog(@"%f,%f",self.securityCodeView.image.size.width,self.securityCodeView.image.size.height);
    [[SDImageCache sharedImageCache]removeImageForKey:self.model.picCaptchaUrl];
    [self.securityCodeView sd_setImageWithURL:[NSURL URLWithString:self.model.picCaptchaUrl] placeholderImage:nil];
    
}




- (void)Countdown{
    
    times--;
    
    if (times == 0) {
        
        times = 60;
        
        [self.timer invalidate];
        
        self.rightBtn.enabled = YES;
        
        self.rightBtn.backgroundColor = themeColor;
        
        self.lineView.backgroundColor = themeColor;

        
        [self.rightBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        
        return;
    }
    
    
    
    [self.rightBtn setTitle:[NSString stringWithFormat:@"已发送(%lds)",(long)times] forState:UIControlStateNormal];
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    

}



@end
